package com.coltsmedia.RichmondHighNews;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;
import android.webkit.WebView;

import com.coltsmedia.RichmondHighNews.R;
import com.coltsmedia.RichmondHighNews.adapter.ArticleListAdapter;
import com.coltsmedia.RichmondHighNews.db.DbAdapter;
import com.coltsmedia.RichmondHighNews.rss.domain.Article;
import com.coltsmedia.RichmondHighNews.util.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;



public class ArticleDetailFragment extends Fragment {

    public static final String ARG_ITEM_ID = "item_id";

    Article displayedArticle;
    DbAdapter db;

    public ArticleDetailFragment() {
    	setHasOptionsMenu(true);	//this enables us to set actionbar from fragment
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        db = new DbAdapter(getActivity());
        if (getArguments().containsKey(Article.KEY)) {
            displayedArticle = (Article) getArguments().getSerializable(Article.KEY);
        }
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_article_detail, container, false);

        TextView textView = (TextView) rootView.findViewById(R.id.article_title);
        textView.setText(displayedArticle.getTitle());

        if (displayedArticle != null) {
        	String title = displayedArticle.getTitle();
        	String pubDate = displayedArticle.getPubDate();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
            try {
				Date pDate =  df.parse(pubDate);
				pubDate = "This post was published " + DateUtils.getDateDifference(pDate) + " by " + displayedArticle.getAuthor();
			} catch (ParseException e) {
				Log.e("DATE PARSING", "Error parsing date..");
                pubDate = "published by " + displayedArticle.getAuthor();
			}

            if (textView.getText().toString().equals("Bell Schedule")){
                pubDate = "Schedule updated every morning";
                textView.setTypeface(Typeface.DEFAULT_BOLD);
            }


            ((TextView) rootView.findViewById(R.id.article_title)).setText(title);
        	((TextView) rootView.findViewById(R.id.article_author)).setText(pubDate);
        }

        String content = displayedArticle.getEncodedContent();
        WebView webview = (WebView)rootView.findViewById(R.id.article_detail);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadData(content, "text/html", "UTF-8");
        return rootView;
    }
    
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.detailmenu, menu);
    }
}